﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.SessionState;
using ihongma.Web.UI;
using ihongma.Common;
using ihongma;
using System.Text;
using System.Web.SessionState;
namespace ihongma.Web.tools
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class login : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = DTRequest.GetQueryString("action");

            switch (action)
            {
                case "login": //发布留言
                    actionlogin(context);
                    break;
                case "reg":
                    registration(context);
                    break;
                    
            }
        }
        public void actionlogin(HttpContext context)
        {
            string userName =  DTRequest.GetFormString("texname");
            string userPwd = DTRequest.GetFormString("userPwd");
            string _code = DTRequest.GetFormString("code");
            string returl = DTRequest.GetFormString("returl");
            //校检验证码
            if (string.IsNullOrEmpty(_code))
            {
                context.Response.Write("{msg:0, msgbox:\"对不起，请输入验证码！\"}");
                return;
            }
            if (context.Session[DTKeys.SESSION_CODE] == null)
            {
                context.Response.Write("{msg:0, msgbox:\"对不起，系统找不到生成的验证码！\"}");
                return;
            }
            if (_code.ToLower() != (context.Session[DTKeys.SESSION_CODE].ToString()).ToLower())
            {
                context.Response.Write("{msg:0, msgbox:\"您输入的验证码与系统的不一致！\"}");
                return;
            }
            BLL.manager bll = new BLL.manager();
            Model.manager model = bll.GetModel(userName, DESEncrypt.Encrypt(userPwd));
            if (model == null)
            {
                
                context.Response.Write("{msg:0, msgbox:\"用户名或密码有误！\"}");
                return;
            }
            context.Session[DTKeys.SESSION_ADMIN_INFO] = model;
            context.Session.Timeout = 45;
            //写入登录日志
            Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig(Utils.GetXmlMapPath(DTKeys.FILE_SITE_XML_CONFING));
            if (siteConfig.logstatus > 0)
            {
                Model.manager_log modelLog = new Model.manager_log();
                modelLog.user_id = model.id;
                modelLog.user_name = model.user_name;
                modelLog.action_type = "login";
                modelLog.note = "用户登录";
                modelLog.login_ip = DTRequest.GetIP();
                modelLog.login_time = DateTime.Now;
                new BLL.manager_log().Add(modelLog);
            }
           

            Utils.WriteCookie("AdminName", "ihongma", model.user_name);
            Utils.WriteCookie("AdminPwd", "ihongma", model.user_pwd);
            context.Response.Write("{msg: 1, msgbox: \"恭喜您,登陆成功！<a href="+returl.Replace("aspx/","")+" style='color:Red;'>点击跳转</a>\"}");
           //context.Response.Redirect(returl);
            return;
        }
        public void registration(HttpContext context)
        {

            string _code = DTRequest.GetFormString("code"); ;
            string txtUserName = DTRequest.GetFormString("txtUserName"); 
            string txtUserPwd = DTRequest.GetFormString("txtUserPwd");
            string txtRealName = DTRequest.GetFormString("txtRealName");
            string txtTelephone = DTRequest.GetFormString("txtTelephone");
            string txtEmail = DTRequest.GetFormString("txtEmail"); 
            //校检验证码
            if (string.IsNullOrEmpty(_code))
            {
                context.Response.Write("{msg:0, msgbox:\"对不起，请输入验证码！\"}");
                return;
            }
            if (context.Session[DTKeys.SESSION_CODE] == null)
            {
                context.Response.Write("{msg:0, msgbox:\"对不起，系统找不到生成的验证码！\"}");
                return;
            }
            if (_code.ToLower() != (context.Session[DTKeys.SESSION_CODE].ToString()).ToLower())
            {
                context.Response.Write("{msg:0, msgbox:\"您输入的验证码与系统的不一致！\"}");
                return;
            }
           // bool result = true;
            Model.manager model = new Model.manager();
            BLL.manager bll = new BLL.manager();
           
            model.role_id = 5;
            model.role_type = 2;
            model.is_lock = 1;
            model.user_name = txtUserName;
            model.user_pwd = DESEncrypt.Encrypt(txtUserPwd);
            model.real_name = txtRealName;
            model.telephone = txtTelephone;
            model.email = txtEmail;
            model.add_time = DateTime.Now;
            
            if (bll.Add(model)>0)
            {
                context.Response.Write("{msg: 1, msgbox: \"恭喜您，提交成功啦！<a href='login.aspx' style='color:Red;'>点击跳转下载页</a>\"}");
                return;
            }
            context.Response.Write("{msg: 0, msgbox: \"对不起，保存过程中发生错误！\"}");
            return;
       
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
